package com.tangdi.production.mpcctp.controller;

import com.tangdi.production.mpaychl.dao.AllpayReqContentMapper;
import com.tangdi.production.mpaychl.model.AllpayReqContent;
import com.tangdi.production.mpaychl.model.AllpayReqContentExample;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

@Controller
public class UspayController {
    private static final Logger logger = LoggerFactory.getLogger(UspayController.class);

    @Autowired
    private AllpayReqContentMapper reqContentMapper;

    @RequestMapping("pay/{hashCode}")
    public void alpayHtmlReq(@PathVariable("hashCode") String hashCode, HttpServletRequest request, HttpServletResponse response) {

        AllpayReqContentExample contentExample = new AllpayReqContentExample();
        contentExample.createCriteria().andHashCodeEqualTo(hashCode);
        List<AllpayReqContent> allpayReqContents = reqContentMapper.selectByExampleWithBLOBs(contentExample);

        AllpayReqContent reqContent = new AllpayReqContent();
        reqContent.setReqDttm(new Date());
        reqContentMapper.updateByExampleSelective(reqContent, contentExample);

        response.setContentType("text/html; charset=utf-8");
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
            if (CollectionUtils.isNotEmpty(allpayReqContents)) {

                String formContent = new String(allpayReqContents.get(0).getReqContent());
                logger.info("返回页面为:{}",formContent);
                writer.write(formContent);
            } else {
                logger.error("请求错误");
                writer.write("请求错误");
            }
            writer.flush();
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        } finally {
            writer.close();
        }
    }

    @RequestMapping("/pay/redirect/{hashCode}")
    public ModelAndView wxRedirectReq(@PathVariable("hashCode") String hashCode, HttpServletRequest request, HttpServletResponse response) {

        AllpayReqContentExample contentExample = new AllpayReqContentExample();
        contentExample.createCriteria().andHashCodeEqualTo(hashCode);
        List<AllpayReqContent> allpayReqContents = reqContentMapper.selectByExampleWithBLOBs(contentExample);

        AllpayReqContent reqContent = new AllpayReqContent();
        reqContent.setReqDttm(new Date());
        reqContentMapper.updateByExampleSelective(reqContent, contentExample);

        ModelAndView mav =  new ModelAndView("/weixin/weixinpay");
        mav.addObject("redirect_url", new String(allpayReqContents.get(0).getReqContent()));

        return mav;
    }

}
